import sys
import re

# not used.
quotes = [
    "CHF",
    "DAI",
    "DOT",
    "USDC",
    "USDT",
    "XETH",
    "XXBT",
    "ZAUD",
    "ZCAD",
    "ZEUR",
    "ZGBP",
    "ZJPY",
    "ZUSD",
]

# remove 'X' and 'Z' prefix.
quotes = [
    "CHF",
    "DAI",
    "DOT",
    "USDC",
    "USDT",
    "ETH",
    "XBT",
    "AUD",
    "CAD",
    "EUR",
    "GBP",
    "JPY",
    "USD",
]

special_currency = [
  "AVAX",
  "CHZ",
  "DYDX",
  "ICX",
  "SNX",
  "TRX",
  "XBT",
  "XDG",
  "XLM",
  "XMR",
  "XRP",
  "XTZ",
  "ZEC",
  "ZRX"
]

# extract quote
quotes = '|'.join(quotes)
special_currency = '^(X|Z)?(' + '|'.join(special_currency) + ')(X|Z)?$'

product_re = r'^([A-Z0-9.]{2,12})(' + quotes + ')$' # use this in c++
product_re_cap = r'^(?P<base>[A-Z0-9.]{2,12})(?P<quote>' + quotes + ')$'
# print(product_re)
print(special_currency)

def parse_base(s):
  # match special currency first
  r = re.match(special_currency, s)
  if r:
    return r.group(2)

  #if cannot remove, simply remove prefix and postfix XZ
  if s[0] == 'X' or s[0] == 'Z':
    s = s[1:]

  if s[-1] == 'X' or s[-1] == 'Z':
    s = s[:-1]

  return s


def main():
    try:
      r = re.match(product_re_cap, sys.argv[1])
      r = r.groupdict()
      b = parse_base(r['base'])
      print(b + '/' + r['quote'])
    except Exception as e:
      print("error", sys.argv[1], e)

#main()
